package com.cet.programmercarl.algorithmperformancanalysis.双指针法;

/**
 * @program: algorithm
 * @description: 反转字符串
 * @author: 陈恩涛
 * @create: 2022-03-24 10:46
 **/
public class ReverseString {

    public void reverseString(char[] s) {
        int left = 0;
        int right = s.length - 1;
        while (left < right) {
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }

    public void reverseString2(char[] s) {
        int l = 0;
        int r = s.length - 1;
        while (l < r) {
            // a^b^b = a^(b^b) = a^0 = a
            s[l] ^= s[r];
            s[r] ^= s[l];
            s[l] ^= s[r];
            l++;
            r--;
        }
    }

    public static void main(String[] args) {
        char[] s = "abcdefg".toCharArray();
        new ReverseString().reverseString2(s);
        for (char c : s) {
            System.out.println(c);
        }
    }
}
